<template>
	<view class="container">
		<unicloud-db ref="udb" v-slot:default="{data, pagination, loading, hasMore, error}" :collection="collectionList"
			:page-size='23' :where="`user_id=='${user_ids}'`">
			<view v-if="error">{{error.message}}</view>
			<view v-else-if="data">
				<uni-list>

					<uni-list-item v-for="(item, index) in data" :key="index" showArrow :clickable="true"
						@click="handleItemClick(item._id)">
						<template v-slot:body>
							<text v-for="(el,i) in item.area_code" style="margin-left: 40rpx;">
								<!-- {{item}} -->
								{{item.address}}
							</text>
						</template>
						<template v-slot:header>
							<text>
								<!-- 此处默认显示为_id，请根据需要自行修改为其他字段 -->
								<!-- 如果使用了联表查询，请参考生成的 admin 项目中 list.vue 页面 -->
								<!-- {{item}} -->
								{{item.name}}
							</text>
						</template>
					</uni-list-item>
				</uni-list>
			</view>
			<uni-load-more :status="loading?'loading':(hasMore ? 'more' : 'noMore')"></uni-load-more>
		</unicloud-db>
		<uni-fab ref="fab" horizontal="right" vertical="bottom" :pop-menu="false" @fabClick="fabClick" />
	</view>
</template>

<script>
	const db = uniCloud.database()
	export default {
		data() {
			return {
				collectionList: [db.collection('uni-id-address').field(
					'user_id,name,alias,mobile,mobile_area_code,province_code,city_code,area_code,address,formatted_address,zip_code,email,is_default'
				).getTemp(), db.collection('opendb-city-china').field(
					'code, name as text, eq(type, 2) as isleaf').getTemp()],
				loadMore: {
					contentdown: '',
					contentrefresh: '',
					contentnomore: ''
				},
				flage: false,
				user_ids: null
			}
		},
		onLoad(opction) {
			this.flage = opction.flg ? true : false
			if (uni.getStorageSync('userInfor')) {
				this.user_ids = uni.getStorageSync('userInfor')._id
			} else {
				uni.showToast({
					title: '请登录',
					icon: 'none'
				})
				uni.navigateTo({
					url: '/pages/login/login'
				})
			}


		},
		onPullDownRefresh() {
			this.$refs.udb.loadData({
				clear: true
			}, () => {
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			this.$refs.udb.loadMore()
		},
		methods: {
			handleItemClick(id) {
				if (this.flage) {
					uni.setStorageSync('selectAddress', id)
					uni.navigateBack()
				} else {
					uni.navigateTo({
						url: './edit?id=' + id
					})
				}
			},
			fabClick() {
				// 打开新增页面
				uni.navigateTo({
					url: './add',
					events: {
						// 监听新增数据成功后, 刷新当前页面数据
						refreshData: () => {
							this.$refs.udb.loadData({
								clear: true
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>